<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\common\command\crontab;

use think\Db;


class Notice
{
    /**
     * User: Simony
     * Function: 任何一个定时任务文件
     */
    public static function execute($functionName)
    {
        self::$functionName();
    }

    public static function evaluateNotice()
    {

        $subQuery = model('common/view/ViewLog')
            ->field('id,member_id,been_member_id,count(id) as count,view_time')
            ->where(['type' => 2])
            ->group('member_id,been_member_id')->buildSql();

        $current_time = time();
        $list = Db::table($subQuery)
            ->alias('s')
            ->field('m.id as b_id,m.nickname as b_nickname,m.sex as b_sex,s.*')//这个member 是被查看的用户
            ->join('member m', 'm.id=s.been_member_id')
            ->where([
                ['s.count', 'eq', 1],
                ['view_time', 'lt', $current_time - 60 * 5],  // view_time < 当前时间 - 5分钟
                ['view_time', 'gt', $current_time - 60 * 6 + 10], //view_time > 当前时间 - 6分钟
            ])
            ->select();

        $add_mess = [];
        foreach ($list as $v) {
            $content = '与【' . $v['b_nickname'] . '】' . '的交流愉快吗？评价一下' . ($v['b_sex'] == 1 ? '他' : '她') . '吧';
            model('common/Noticle', 'logic')->sendTxtSysNotice($v['member_id'], $content, 1);

            $add_mess[] = [
                'member_id' => -1,
                'receive_member_id' => $v['member_id'],
                'type' => 1,
                'relation_member_id' => $v['b_id'],
                'create_time' => $current_time,
                'content' => $content,
            ];
        }

        if (!empty($add_mess)) {
            $messageModel = model('common/message/Message');
             $messageModel->addAll($add_mess);
        }

    }
}


